package weaver.interfaces.cese.duss.action.fee;

import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.cese.duss.ceseutil.ReserveUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-10-31
 * @Description: 备用金申请
 * @ModifyLog:
 **/

public class WeaReserveApplicatAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        RecordSet rs = new RecordSet();
        writeLog( "----WeaReserveApplicatAction-------" + requestid );
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        try {
            String sql = "select jkr,jkfb,jkbm,fylx,xmbm,xmmc,xmje,yjghrq,xmwcrq,jksy,grbyj,xmbyj,spje from " + tablename + " where requestid=?";
            rs.executeQuery( sql, requestid );
            writeLog( ">>>>>sql" + sql );
            rs.next();
            String jkr = Util.null2String( rs.getString( "jkr" ) );//借款人
            String jkfb = Util.null2String( rs.getString( "jkfb" ) );//借款分部
            String jkbm = Util.null2String( rs.getString( "jkbm" ) );//借款部门
            String fylx = Util.null2String( rs.getString( "fylx" ) );//费用类型
            String xmbm = Util.null2String( rs.getString( "xmbm" ) );//项目编码
            String xmmc = Util.null2String( rs.getString( "xmmc" ) );//项目名称
            String xmje = Util.null2String( rs.getString( "xmje" ) );//项目金额
            String yjghrq = Util.null2String( rs.getString( "yjghrq" ) );//预计归还日期
            String jksy = Util.null2String( rs.getString( "jksy" ) );//借款事由
            String xmwcrq = Util.null2String( rs.getString( "xmwcrq" ) );//项目完成日期
            String spje = Util.null2String( rs.getString( "spje" ) );//审批金额
            String PERSON = "0";//个人
            String PROJECT = "1";//项目
            //费用类型为个人
            if(PERSON.equals(fylx)){
                writeLog( "费用类型为个人");
                if ( ReserveUtil.isExistByj(fylx,jkr)) {
                    double yfsje =  ReserveUtil.calReserveBorrow( fylx, xmbm,jkr);
                    String sql1 = "update uf_byjglr set  yfsje=? where byjlx=? and byjzrr=?";
                    rs.executeUpdate(sql1,new Object[]{yfsje,fylx,jkr});
                    rs.writeLog(sql1,String.join( ",",yfsje+"",fylx,jkr));
                }else {//项目
                    String sql1 = "insert into uf_byjglr (ssbm,byjlx,byjzrr,ssfb,xmbm,xmmc,xmje,hkje,yfsje,yjhkrq,xmwcrq,bz,byjcs,formmodeid)" +
                            "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                    rs.executeUpdate(sql1,new Object[]{jkbm,fylx,jkr,jkfb,xmbm,xmmc,xmje,0,spje,yjghrq,xmwcrq,jksy,0,ReserveUtil.getFormmodeid()});
                    rs.writeLog(sql1,String.join( ",",jkbm,fylx,jkr,jkfb,xmbm,xmmc,xmje,0+"",spje,yjghrq,xmwcrq,jksy,0+"",ReserveUtil.getFormmodeid()));
                }
            }
            //费用类型为项目
            if(PROJECT.equals(fylx)) {
                writeLog( "费用类型为项目");
                if ( ReserveUtil.isExistByj(fylx,xmbm)) {
                    double yfsje = ReserveUtil.calReserveBorrow(fylx, xmbm,jkr);
                    String sql2 = "update uf_byjglr set yfsje=? where byjlx=? and xmbm=?";
                    rs.executeUpdate(sql2,new Object[]{yfsje,fylx,xmbm});
                    rs.writeLog(sql2,String.join( ",",yfsje+"",fylx,xmbm));
                }else {
                    String sql2 = "insert into uf_byjglr (ssbm,byjlx,byjzrr,ssfb,xmbm,xmmc,xmje,hkje,yfsje,yjhkrq,xmwcrq,bz,byjcs,formmodeid)" +
                            "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

                    rs.executeUpdate(sql2,new Object[]{jkbm,fylx,jkr,jkfb,xmbm,xmmc,xmje,0,spje,yjghrq,xmwcrq,jksy,0,ReserveUtil.getFormmodeid()});
                    rs.writeLog(sql2,String.join( ",",jkbm,fylx,jkr,jkfb,xmbm,xmmc,xmje,0+"",0+"",0+"",spje,yjghrq,xmwcrq,jksy,0+"",ReserveUtil.getFormmodeid()));
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }

    }
}
